//package com.tj.bo.integration.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.amazonaws.auth.AWSStaticCredentialsProvider;
//import com.amazonaws.auth.BasicAWSCredentials;
//import com.amazonaws.regions.Regions;
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Service;
//
//import javax.activation.DataHandler;
//import javax.activation.DataSource;
//import javax.activation.FileDataSource;
//import java.io.ByteArrayOutputStream;
//import java.nio.ByteBuffer;
//import java.util.Arrays;
//import java.util.Objects;
//import java.util.Properties;
//import java.util.stream.Collectors;
//
///**
// * Created with IntelliJ IDEA.
// * User: sm
// * Date: 2021/7/2
// * Time: 11:10 AM
// */
//@Service
//public class AmazonEmailService {
//    private static final Logger log = LoggerFactory.getLogger(AmazonEmailService.class);
//
//    public String sendMailSingle(EmailConfigVO emailConfigVO, MsgInfoVO msgInfo) {
//        if (msgInfo == null) {
//            log.info("NO DATA NEED HAND");
//            return null;
//        }
//        try {
//            Session session = Session.getDefaultInstance(new Properties());
//            log.info("Attempting to send an email through Amazon SES using the AWS SDK for Java...");
//            BasicAWSCredentials credentials = new BasicAWSCredentials(emailConfigVO.getAccessKey(), emailConfigVO.getSecretKey());
//            AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
//                    .withCredentials(new AWSStaticCredentialsProvider(credentials))
//                    .withRegion(Regions.US_EAST_1).build();
//            MimeMessage message = buildMessage(msgInfo.getTitle(), msgInfo.getContent(), "", session, emailConfigVO.getAccountName(), msgInfo.getEmail());
//            SendRawEmailRequest request = buildRequest(message);
//            SendRawEmailResult response = client.sendRawEmail(request);
//            if (Objects.isNull(response) || Objects.isNull(response.getSdkHttpMetadata())) {
//                return null;
//            }
//            // 错误码参见： https://docs.aws.amazon.com/zh_cn/ses/latest/DeveloperGuide/api-error-codes.html
//            log.info("send amazon email request:{},result:{}", JSONObject.toJSONString(request), JSONObject.toJSONString(response));
//            if (200 == response.getSdkHttpMetadata().getHttpStatusCode()) {
//                return response.getMessageId();
//            }
//            return null;
//        } catch (Exception e) {
//            log.error("The email was not sent, Error message: " + e.getMessage());
//        }
//        return null;
//    }
//
//    private SendRawEmailRequest buildRequest(MimeMessage message) throws Exception {
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        message.writeTo(outputStream);
//        RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
//        return new SendRawEmailRequest(rawMessage);
//    }
//
//    public static void main(String[] args) throws Exception {
//        String s = HttpUtils.getPage("https://qianduohui.oss-cn-shanghai.aliyuncs.com/hunter/base/basicTemplate.htm");
//        AmazonEmailService amazonEmailService = new AmazonEmailService();
//        MsgInfoVO msgInfoVO = new MsgInfoVO();
//        msgInfoVO.setEmail("karl@tangji-inc.com");
//        msgInfoVO.setTitle("你好测试邮件");
//        msgInfoVO.setContent("你的验证码是1112312312312,10分钟内有效哈哈哈哈哈所发所绿多军");
//        String ret = amazonEmailService.sendMailSingle(new EmailConfigVO(), msgInfoVO);
//        System.out.println(ret);
//    }
//
//    private MimeMessage buildMessage(String textSubject, String textBody, String filePath, Session session, String fromEmail, String... toEmail) throws Exception {
//        MimeMessage message = new MimeMessage(session);
//        message.setSubject(textSubject, "UTF-8");
//        message.setFrom(new InternetAddress(fromEmail));
//        message.setRecipients(RecipientType.TO,
//                Arrays.stream(toEmail).collect(Collectors.joining(", ")));
//
//        MimeMultipart body = new MimeMultipart("alternative");
//
//        //        可能有些邮箱客户端不支持接收html， 可以直接发送文件邮件（优先级：html无法解析时， 才会接收text格式邮件）
//        MimeBodyPart textPart = new MimeBodyPart();
//        textPart.setContent(textBody, "text/plain; charset=UTF-8");
//        textPart.setHeader("Content-Transfer-Encoding", "base64");
//        body.addBodyPart(textPart);
//
//
//        // Define the HTML part.
//        MimeBodyPart htmlPart = new MimeBodyPart();
//        // Encode the HTML content and set the character encoding.
//        htmlPart.setContent(textBody, "text/html; charset=UTF-8");
//        htmlPart.setHeader("Content-Transfer-Encoding", "base64");
//
//        // Add the text and HTML parts to the child container.
//        body.addBodyPart(htmlPart);
//
//
//        if (StringUtils.isNotBlank(filePath)) {
//            MimeBodyPart attachmentPart = new MimeBodyPart();
//            DataSource fileDataSource = new FileDataSource(filePath);
//            attachmentPart.setDataHandler(new DataHandler(fileDataSource));
//            attachmentPart.setFileName(fileDataSource.getName());
//
//            body.addBodyPart(attachmentPart);
//        }
//        message.setContent(body);
//
//
//        return message;
//    }
//}
